package com.wechat.shopping.mall.account.service.impl;


import com.wechat.shopping.mall.account.domain.SaveCustomerParam;
import com.wechat.shopping.mall.account.service.CustomerInfoService;
import com.wechat.shopping.mall.bean.po.WechatShoppingCustomer;
import com.wechat.shopping.mall.bean.po.WechatShoppingCustomerExample;
import com.wechat.shopping.mall.bean.po.WechatShoppingUser;
import com.wechat.shopping.mall.bean.po.WechatShoppingUserExample;
import com.wechat.shopping.mall.po.WechatShoppingCustomerMapper;
import com.wechat.shopping.mall.po.WechatShoppingUserMapper;
import com.wechat.shopping.mall.util.BusinessException;
import com.wechat.shopping.mall.util.Constant;
import com.wechat.shopping.mall.util.CreatePathUtil;
import com.wechat.shopping.mall.util.MD5Util;
import com.wechat.shopping.mall.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional(readOnly = true)
public class CustomerInfoServiceImpl implements CustomerInfoService{
    private static final Logger LOG = LoggerFactory.getLogger(CustomerInfoServiceImpl.class);

    @Autowired
    private WechatShoppingCustomerMapper wechatShoppingCustomerMapper;

    @Autowired
    private WechatShoppingUserMapper wechatShoppingUserMapper;

    @Override
    public ResponseMessage saveCustomer(SaveCustomerParam saveCustomerParam) {
        try {
            //校验数据
            checkCustomerParam(saveCustomerParam);
            Date date = new Date();
            //父级管理方组织机构详情
            WechatShoppingCustomer parentCustomer = wechatShoppingCustomerMapper.selectByPrimaryKey(saveCustomerParam.getParentId());
            if (null == parentCustomer) {
                throw new BusinessException("机构管理方不存在!");
            }
            //创建组织机构
            WechatShoppingCustomer customer = new WechatShoppingCustomer();
            BeanUtils.copyProperties(saveCustomerParam,customer);
            customer.setCreateId(saveCustomerParam.getUserId());
            customer.setCreateTime(date);
            //获取组织机构层级 层级到上级组织机构ID
            customer.setPath(CreatePathUtil.getChildPath(parentCustomer));
            wechatShoppingCustomerMapper.insertSelective(customer);
            //生成默认管理员
            WechatShoppingUser adminUser = new WechatShoppingUser();
            BeanUtils.copyProperties(saveCustomerParam,adminUser);
            adminUser.setCreateId(saveCustomerParam.getUserId());
            adminUser.setCreateTime(date);
            //密码加密
            adminUser.setPassword(MD5Util.textToMD5L32(saveCustomerParam.getPassword()));
            //默认管理员与默认权限组关系绑定
            adminUser.setRoleId(Constant.ONE_INTEGER);
            //默认管理员与组织机构绑定关系
            adminUser.setCustomerId(customer.getId());
            wechatShoppingUserMapper.insertSelective(adminUser);
            return ResponseMessage.ok();
        } catch (Exception e) {
           LOG.error("创建组织机构异常",e);
           return ResponseMessage.error("创建组织机构错误");
        }
    }

    /**
     * 创建组织机构参数校验
     * @param saveCustomerParam
     */
    private void checkCustomerParam(SaveCustomerParam saveCustomerParam) {
        //组织机构名称不可重复
        WechatShoppingCustomerExample wechatShoppingCustomerExample = new WechatShoppingCustomerExample();
        wechatShoppingCustomerExample.createCriteria()
                .andCustomerNameEqualTo(saveCustomerParam.getCustomerName())
                .andIsdelEqualTo(Constant.ZERO_BYTE);
        WechatShoppingCustomer customerNameCustomer = wechatShoppingCustomerMapper.selectByExample(wechatShoppingCustomerExample)
                .stream().findFirst().orElse(null);
        if (null != customerNameCustomer) {
            throw new BusinessException("组织机构名称重复,请修改!");
        }
        //组织机构简称不可重复
        wechatShoppingCustomerExample.clear();
        wechatShoppingCustomerExample.createCriteria()
                .andCustomerNameEqualTo(saveCustomerParam.getCustomerNameShort())
                .andIsdelEqualTo(Constant.ZERO_BYTE);;
        WechatShoppingCustomer customerNameShortCustomer = wechatShoppingCustomerMapper.selectByExample(wechatShoppingCustomerExample)
                .stream().findFirst().orElse(null);
        if (null != customerNameShortCustomer) {
            throw new BusinessException("组织机构简称重复,请修改!");
        }
        //管理员账号不可以重复
        WechatShoppingUserExample wechatShoppingUserExample = new WechatShoppingUserExample();
        wechatShoppingUserExample.createCriteria()
                .andUserAccountEqualTo(saveCustomerParam.getUserAccount())
                .andIsdelEqualTo(Constant.ZERO_BYTE);
        WechatShoppingUser accountUser = wechatShoppingUserMapper.selectByExample(wechatShoppingUserExample)
                .stream().findFirst().orElse(null);
        if (null == accountUser) {
            throw new BusinessException("管理员账号重复,请修改!");
        }
    }
}
